<?php
declare (strict_types=1);

namespace app\admin\controller;

use think\facade\View;
use think\response\Json;

class AttributeValue extends AdminBase
{
    public function lists(): string
    {
        $attributeId = $this->request->param('attribute_id', 0, 'intval');
        if (empty($attributeId)) {
            View::assign(['msg' => self::_PARAMETER_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }

        $attributeBusiness = new \app\common\business\Attribute($this->websiteCode);
        $attribute         = $attributeBusiness->findAttributeById($attributeId);
        if (empty($attribute)) {
            View::assign(['msg' => self::_PARAMETER_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }

        View::assign([
            'attributeId'      => $attributeId,
            "attributeName"    => $attribute['name'],
            'AttributeListUrl' => url('attribute/lists', ['website_code' => $this->websiteCode])]);

        return View::fetch();
    }

    public function list(): Json
    {
        $attributeId            = $this->request->param('attribute_id', 0, 'intval');
        $page                   = $this->request->param('page', 1, 'intval');
        $limit                  = $this->request->param('limit', 10, 'intval');
        $attributeValue         = $this->request->param('attribute_value', '', 'trim');
        $attributeValueBusiness = new \app\common\business\AttributeValue($this->websiteCode);

        $where = ['attribute_id' => $attributeId];
        if ($attributeValue) {
            $where[] = ['attribute_value', 'like', '%' . $attributeValue . '%'];
        }

        $attribute = $attributeValueBusiness->getAttributeValueList($page, $limit, $where);
        return showList(config(self::_CONFIG_STATUS_SUCCESS_CODE), 'success', $attribute['rows'], $attribute['count']);
    }

    public function add(): string
    {
        $attributeId = $this->request->param('attribute_id', 0, 'intval');
        if (empty($attributeId)) {
            View::assign(['msg' => self::_PARAMETER_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }

        View::assign(['attributeId' => $attributeId]);
        return View::fetch();
    }

    public function doAdd(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $attributeId    = $this->request->param('attribute_id', 0, 'intval');
        $remark         = $this->request->param('remark', '', 'trim');
        $attributeValue = $this->request->post('attribute_value', '', 'trim');
        $sort           = $this->request->param('sort', 0, 'intval');

        $data     = [
            'attribute_id'    => $attributeId,
            'remark'          => $remark,
            'attribute_value' => $attributeValue,
            'sort'            => $sort
        ];
        $validate = (new \app\admin\validate\AttributeValue())->scene('add');
        if (!$validate->check($data)) {
            return show(config("status.error"), $validate->getError());
        }

        $attributeValueBusiness = new \app\common\business\AttributeValue($this->websiteCode);
        $res                    = $attributeValueBusiness->addAttributeValue($data);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }

    public function edit(): string
    {
        $id = $this->request->param('id', 0, 'intval');
        if (!$id) {
            View::assign(['msg' => self::_PARAMETER_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }

        $attributeValueBusiness = new \app\common\business\AttributeValue($this->websiteCode);
        $attributeValue         = $attributeValueBusiness->findAttributeValueById($id);
        if (empty($attributeValue)) {
            View::assign(['msg' => self::_PARAMETER_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }
        View::assign(["attributeValue" => $attributeValue]);

        return View::fetch();
    }

    public function doEdit(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $id             = $this->request->param('id', 0, 'intval');
        $remark         = $this->request->param('remark', '', 'trim');
        $attributeValue = $this->request->post('attribute_value', '', 'trim');
        $sort           = $this->request->param('sort', 0, 'intval');

        $data     = [
            'remark'          => $remark,
            'attribute_value' => $attributeValue,
            'sort'            => $sort
        ];
        $validate = (new \app\admin\validate\AttributeValue())->scene('edit');
        if (!$validate->check($data)) {
            return show(config("status.error"), $validate->getError());
        }

        $attributeValueBusiness = new \app\common\business\AttributeValue($this->websiteCode);
        $res                    = $attributeValueBusiness->updateAttributeValue($id, $data);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }

    public function doDelete(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $id = $this->request->param('id', 0, 'intval');
        if ($id == 0) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        $attributeValueBusiness = new \app\common\business\AttributeValue($this->websiteCode);
        $res                    = $attributeValueBusiness->deleteAttributeValue($id);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }

    public function doSort(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $ids  = $this->request->param('ids', '', 'trim');
        $sort = $this->request->param('sort', '', 'trim');
        if (empty($ids) || empty($sort)) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        $idArr = explode(',', $ids);
        foreach ($idArr as $k => $v) {
            $idArr[$k] = intval($v);
        }

        $sortArr = explode(',', $sort);
        foreach ($sortArr as $k => $v) {
            $sortArr[$k] = intval($v);
        }

        $moduleFieldBusiness = new \app\common\business\AttributeValue($this->websiteCode);
        foreach ($idArr as $k => $v) {
            $moduleFieldBusiness->updateAttributeValue($v, ['sort' => $sortArr[$k]]);
        }

        return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), '排序成功');
    }

    public function batchDoDelete(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $ids = $this->request->param("ids");
        if ($ids == "") {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }
        $ids = explode(",", $ids);
        foreach ($ids as $k => $v) {
            $ids[$k] = intval($v);
        }

        $feedbackFormBusiness = new \app\common\business\AttributeValue($this->websiteCode);
        $res                  = $feedbackFormBusiness->batchDeleteAttributeValue($ids);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }
}
